﻿var map;
require(["esri/map",
		 "esri/graphic",
		 "esri/InfoTemplate",
         "esri/dijit/Scalebar",
         "esri/SpatialReference",
         "esri/geometry/Polyline",
         "esri/geometry/Polygon",
         "esri/symbols/SimpleLineSymbol",
         "esri/symbols/SimpleFillSymbol",
	     "esri/layers/GraphicsLayer",
	     "esri/geometry/geodesicUtils",
	      "esri/Color",
          "dojo/request",
         "dojo/domReady!"], function(Map, Graphic, InfoTemplate, Scalebar, SpatialReference, Polyline, Polygon,
        		 SimpleLineSymbol ,SimpleFillSymbol, GraphicsLayer, geodesicUtils, Color){
	map = new Map("mainMapDiv",{
		center : [104.078, 30.54],
		scale:5000,
		basemap : "osm",
		fadeOnZoom : true,
		logo : false
	});
	
	var scalebar = new Scalebar({
		map:map,
		attachTo:"bottom-left",//比例尺位置
		scalebarUnit: "metric" //比例尺单位（metric米，默认english）
	});
	scalebar.show();
	
	map.on("dbl-click", function(e){
		console.log(e);
		console.log(e.mapPoint.x+","+e.mapPoint.y);
	});
	
	//渲染自定义数据
	var getSimpleLineSymbolStyle = function(style){
		if(style==="STYLE_SOLID"){
			return SimpleLineSymbol.STYLE_SOLID;
		}
		
		return SimpleLineSymbol.STYLE_DASHDOT;
	}
	
	//定义图层
	var fillsymbolgraphicslayer = new GraphicsLayer({opacity:0.20});
	
	//几何图形存储集合
	var polygons = {};
	
	function createMarkPointGraphic(point, path, color){
          var markerSymbol = new esri.symbol.SimpleMarkerSymbol();
          markerSymbol.setPath(path);
          markerSymbol.setColor(new dojo.Color(color));
          markerSymbol.setOutline(null);
          var graphic = new Graphic(new esri.geometry.Point(point[0], point[1]), markerSymbol);
          return graphic;
    }
	
	var paintFarm = function(wkid, outline, fillsymble, rings, infodata, markPoints){
		if(rings==null || rings.length<4) return;
		var _outlineSymbol = new SimpleLineSymbol(getSimpleLineSymbolStyle(outline.style), outline.color, outline.width);
		
		var sfs = new SimpleFillSymbol(getSimpleLineSymbolStyle(fillsymble.style),
			    _outlineSymbol, 
			    new Color(fillsymble.color)
			  );
		var polygon = new Polygon({
			"rings": [rings],
			"spatialReference":{"wkid":wkid}
		});
		polygons[infodata.landId+"polygon"] = polygon;
		
		var infoTemplate = new InfoTemplate("${landName}","LandId:${landId}<br/>LandCode:${landCode}");
		var graphic = new Graphic(polygon, sfs, infodata, infoTemplate);
		fillsymbolgraphicslayer.add(graphic);
		
		if(markPoints!=null && markPoints.length>0){
			var iconPath = "M24.0,2.199C11.9595,2.199,2.199,11.9595,2.199,24.0c0.0,12.0405,9.7605,21.801,21.801,21.801c12.0405,0.0,21.801-9.7605,21.801-21.801C45.801,11.9595,36.0405,2.199,24.0,2.199zM31.0935,11.0625c1.401,0.0,2.532,2.2245,2.532,4.968S32.4915,21.0,31.0935,21.0c-1.398,0.0-2.532-2.2245-2.532-4.968S29.697,11.0625,31.0935,11.0625zM16.656,11.0625c1.398,0.0,2.532,2.2245,2.532,4.968S18.0555,21.0,16.656,21.0s-2.532-2.2245-2.532-4.968S15.258,11.0625,16.656,11.0625zM24.0315,39.0c-4.3095,0.0-8.3445-2.6355-11.8185-7.2165c3.5955,2.346,7.5315,3.654,11.661,3.654c4.3845,0.0,8.5515-1.47,12.3225-4.101C32.649,36.198,28.485,39.0,24.0315,39.0z";
          	var initColor = "#ce641d";
          	for(var i=0;i<markPoints.length;i++){
          		var point = markPoints[i];
	            map.graphics.add(createMarkPointGraphic(point, iconPath, initColor));
          	}
		}
		
	  	/**
	  	 * 几何图形面积测量
	  	 */
	  	var area = 0;
	  	if(graphic.geometry.type == "polygon"){
	        area = geodesicUtils.geodesicAreas([graphic.geometry], //几何图形对象
	        	esri.Units.SQUARE_METERS//面积单位(平方米)
	        	);
	  	}
	  	
		var _lihtml = "<li><a href='javascript:void(0)' landId='"+
				infodata.landId+"'>"+
				infodata.landName+
				"</a> area["+(Math.abs(area))+" ㎡]</li>";
		$("#landList").append(_lihtml);
	}
	
	dojo.xhrGet({
		url : "./landlist",
		handleAs : "json",
		handle:function(data){
			if(data.records!=null){
				for(var i=0;i<data.records.length;i++){
					var wkid = 4326;
					var outline = {//边界样式
						style : "STYLE_SOLID",
						color : "#0A8F25",
						width : 2
					};
					var fillsymble = {//填充块样式
						style : "STYLE_SOLID",
						color : "#0A8F25"
					};
					var record = data.records[i];
					paintFarm(wkid, outline, fillsymble, record.rings, record.infodata, record.markPoints);
					map.addLayer(fillsymbolgraphicslayer);
				}
			}
		}
	});
	
	$("#landList").on("click", "li a", function(){
		var landId = $(this).attr("landId");
		var polygon_key = landId+"polygon";
	    if(polygon_key in polygons){
		    var polygon = polygons[polygon_key];
		    map.setExtent(polygon.getExtent(), true);//缩放到选择地块
	    }else{
	    	alert("can't locate this land");
	    }
	});
	
});